/* 
 *	Copyright (C) 2009-2010 miaohua
 *	http://code.google.com/p/lrcshower/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "StdAfx.h"
#include "AppMsgDef.h"
#include "SimpleTray.h"


CSimpleTray::CSimpleTray()
:m_Icon(NULL), m_IsEnabled(FALSE)
{}

CSimpleTray::~CSimpleTray()
{
     Hide();
}

void CSimpleTray::Show()
{
     // Check to see if we have a valid icon
     if( m_Icon == NULL )
	     return;

     // Make sure we are not already visible
     if( m_IsEnabled == TRUE )
	     return;

     // Initialize the TRAYNOTIFYDATA struct
     m_NotifyData.cbSize = sizeof(m_NotifyData);
     // 'Owner' window 	that receives notifications
     m_NotifyData.hWnd	 = AfxGetMainWnd()->m_hWnd;	
     m_NotifyData.uID	 = 128;	// ID of our icon, as you can have
						        // more than one icon in a single app
     // User defined callback message
     m_NotifyData.uCallbackMessage = WM_TRAYNOTIFY;	
     // Set tooltip
     _tcscpy_s(m_NotifyData.szTip, _countof(m_NotifyData.szTip), m_ToolTip);	
     m_NotifyData.hIcon  = m_Icon;
     m_NotifyData.uFlags = NIF_MESSAGE |	// flags are set to indicate what
				           NIF_ICON |		// needs to be updated, in this case
				           NIF_TIP;		    // callback message, icon, and tooltip
     // Add system tray icon
     Shell_NotifyIcon(NIM_ADD, &m_NotifyData);	
     // Set status
     m_IsEnabled = TRUE;
}

void CSimpleTray::Hide()
{
     // Make sure we are enabled
     if( m_IsEnabled == TRUE )
     {
	     // Remove
	     m_NotifyData.uFlags = 0;
	     Shell_NotifyIcon(NIM_DELETE, &m_NotifyData);
	     // Set status
	     m_IsEnabled = FALSE;
     }
}

void CSimpleTray::SetIcon(HICON Icon)
{
     // First make sure we get a valid icon
     if( Icon == NULL )
	     return;

     // Set it as our private
     m_Icon = Icon;

     // now check if we are already enabled
     if( m_IsEnabled == TRUE )
     {	
         // Update
	     m_NotifyData.hIcon = m_Icon;
         // Set only icon
	     m_NotifyData.uFlags = NIF_ICON;	
	     Shell_NotifyIcon(NIM_MODIFY, &m_NotifyData);
     }
}

void CSimpleTray::SetToolTip(UINT ID)
{
     CString ToolTip;
     ToolTip.LoadString(ID);
     SetToolTip(ToolTip);
}

void CSimpleTray::SetToolTip(LPCTSTR ToolTip)
{
     // tooltip can be empty, we don't care
     m_ToolTip = ToolTip;

     // if we are enabled
     if( m_IsEnabled == TRUE )
	 {	
         // Do an update
		 _tcscpy_s(m_NotifyData.szTip, _countof(m_NotifyData.szTip), m_ToolTip);
         // Set tooltip only
		 m_NotifyData.uFlags = NIF_TIP;	
		 Shell_NotifyIcon(NIM_MODIFY, &m_NotifyData);
	 }
}

BOOL CSimpleTray::ShowBalloonTip(LPCTSTR Msg, LPCTSTR Title, UINT Timeout, DWORD InfoFlags)
{ 
     m_NotifyData.cbSize = sizeof(NOTIFYICONDATA);
     m_NotifyData.uFlags = m_NotifyData.uFlags|NIF_INFO;
     m_NotifyData.uTimeout = Timeout;
     m_NotifyData.dwInfoFlags = InfoFlags;
     _tcscpy_s(m_NotifyData.szInfo, _countof(m_NotifyData.szInfo), Msg ? Msg : _T(""));
     _tcscpy_s(m_NotifyData.szInfoTitle, _countof(m_NotifyData.szInfoTitle), Title ? Title : _T(""));
     return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyData);
}

BOOL CSimpleTray::ShowBalloonTip(UINT ID, LPCTSTR Title, UINT Timeout, DWORD InfoFlags)
{
     CString Msg;
     return Msg.LoadString(ID) ? ShowBalloonTip(Msg, Title, Timeout, InfoFlags) : FALSE;
}
